package com.mk.admin.service.impl;

import com.mk.admin.mapper.InHospitalReservationMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.dto.InHospitalReservationDto;
import com.mk.admin.model.entity.InHospitalReservationEntity;
import com.mk.admin.model.query.InHospitalReservationQuery;
import com.mk.admin.model.vo.InHospitalReservationVo;
import com.mk.admin.service.InHospitalReservationService;
import com.mk.admin.util.DateTool;
import com.mk.admin.util.JSONCopy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InHospitalReservationServiceImpl implements InHospitalReservationService {
    @Autowired
    private InHospitalReservationMapper inHospitalReservationMapper;

    @Override
    public ResultDto select(InHospitalReservationQuery query) {
        InHospitalReservationEntity entity = JSONCopy.copy(query, InHospitalReservationEntity.class);
        return ResultDto.data(inHospitalReservationMapper.count(entity), inHospitalReservationMapper.select(entity));
    }

    @Override
    public ResultDto add(InHospitalReservationVo inHospitalReservationVo) {
//        先查询当前患者是否医技预约，
        InHospitalReservationEntity entity = JSONCopy.copy(inHospitalReservationVo, InHospitalReservationEntity.class);
        InHospitalReservationEntity temp = new InHospitalReservationEntity();
        temp.setPatId(entity.getPatId());
        temp.setOrderBy("id desc");
        temp.setStatus(1);
        List<InHospitalReservationDto> list = inHospitalReservationMapper.select(entity);
        if (list.size() == 0) {
            entity.setCreateTime(DateTool.format());
            return ResultDto.data(inHospitalReservationMapper.insert(entity), entity);
        }
        InHospitalReservationEntity query = new InHospitalReservationEntity();
        query.setPatId(entity.getPatId());
        temp.setOrderBy("id desc");
        return ResultDto.data(inHospitalReservationMapper.select(query));
    }

    @Override
    public ResultDto update(InHospitalReservationVo inHospitalReservationVo) {
        InHospitalReservationEntity entity = JSONCopy.copy(inHospitalReservationVo, InHospitalReservationEntity.class);
        return ResultDto.data(inHospitalReservationMapper.updateById(entity));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(inHospitalReservationMapper.deleteById(id));
    }

    @Override
    public ResultDto status(int id) {
        return ResultDto.data(inHospitalReservationMapper.status(id));
    }
}
